﻿using System.Windows.Media;

namespace System.Xml.Serialization.TestProject
{
    internal sealed class ColorRuntimeSerializer : XmlRuntimeSerializer<Color>
    {
        private ColorRuntimeSerializer() { }
        private static ColorRuntimeSerializer _instance = new ColorRuntimeSerializer();
        public static ColorRuntimeSerializer Instance { get { return _instance; } }
        protected override void Write(Color value, IXmlPropertyWriter writer, string propertyName)
        {
            Byte.Serialize(writer, "A", value.A);
            Byte.Serialize(writer, "R", value.R);
            Byte.Serialize(writer, "G", value.G);
            Byte.Serialize(writer, "B", value.B);
        }
        protected override Color Read(IXmlPropertyReader reader, string propertyName)
        {
            return new Color()
            {
                A = Byte.Deserialize(reader, "A"),
                R = Byte.Deserialize(reader, "R"),
                G = Byte.Deserialize(reader, "G"),
                B = Byte.Deserialize(reader, "B")
            };
        }
    }

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public sealed class XmlColorRuntimeSerializerAttribute : XmlRuntimeSerializerAttribute
    {
        public XmlColorRuntimeSerializerAttribute()
            : base(ColorRuntimeSerializer.Instance)
        {
        }
    }
}
